class Admin::LeagueGamesController < AdminController
  def index
    @league_games = LeagueGame.all
    if params[:search_key].presence
      @league_games = @league_games.where("name LIKE ?", "%#{params[:search_key]}%")
    end
    @league_games = @league_games.order("created_at DESC").page params[:page]
  end

  def new
    @league_game = LeagueGame.new
  end

  def create
    @league_game = LeagueGame.new league_game_params
    if @league_game.save
      redirect_to admin_league_games_path, notice: "保存成功！"
    else
      redirect_to admin_league_games_path, notice: "保存失败！"
    end
  end

  def edit
    @league_game = LeagueGame.find params[:id]
  end

  def update
    @league_game = LeagueGame.find params[:id]
    if @league_game.update league_game_params
      redirect_to admin_league_games_path, notice: "更新成功！"
    else
      redirect_to admin_league_games_path, notice: "更新失败！"
    end
  end
  
  def destroy
    @league_game = LeagueGame.find params[:id]
    if @league_game.destroy
      redirect_to admin_league_games_path, notice: "删除成功."
    else
      redirect_to admin_league_games_path, notice: "删除失败."
    end
  end

  private

  def league_game_params
    params.require(:league_game).permit(:name)
  end
end
